home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / Sources / FWPrvRFl.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  13.5 KB  |  465 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:        FWPrvRFl.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef   FWPRVRFL_H
  13. #include "FWPrvRFl.h"
  14. #endif
  15.  
  16. #ifndef   FWSTRS_H
  17. #include "FWStrs.h"
  18. #endif
  19.  
  20. #ifndef   FWFILESP_H
  21. #include "FWFileSp.h"
  22. #endif
  23.  
  24. #ifndef FWRESFIL_H
  25. #include "FWResFil.h"
  26. #endif
  27.  
  28. #ifndef FWEXCDEF_H
  29. #include "FWExcDef.h"
  30. #endif
  31.  
  32. #ifndef   FWPRIDEB_H
  33. #include "FWPriDeb.h"
  34. #endif
  35.  
  36. #ifndef FWBNDSTR_H
  37. #include "FWBndStr.h"
  38. #endif
  39.  
  40. //----------------------------------------------------------------------------------------
  41. // Mac-only
  42. //----------------------------------------------------------------------------------------
  43. #ifdef FW_BUILD_MAC
  44.  
  45. #pragma segment FWResource_PrivateResourceFile
  46.  
  47. #ifndef __ERRORS__
  48. #include <errors.h>
  49. #endif
  50.  
  51. #ifndef __ICONS__
  52. #include <Icons.h>
  53. #endif
  54.  
  55. #ifndef __RESOURCES__
  56. #include <Resources.h>
  57. #endif
  58.  
  59. #ifndef __MENUS__
  60. #include <Menus.h>
  61. #endif
  62.  
  63. #ifndef __STRING__
  64. #include <string.h>
  65. #endif
  66.  
  67. #ifndef __MEMORY__
  68. #include <Memory.h>
  69. #endif
  70.  
  71. #ifndef __TOOLUTILS__
  72. #include <ToolUtils.h>
  73. #endif
  74.  
  75. #endif
  76.  
  77. #if FW_LIB_EXPORT_PRAGMAS
  78. #pragma lib_export on
  79. #endif
  80.  
  81. //========================================================================================
  82. // CLASS FW_PPrivResourceFile
  83. //========================================================================================
  84.  
  85. //----------------------------------------------------------------------------------------
  86. // FW_PPrivResourceFile::FW_PPrivResourceFile
  87. //----------------------------------------------------------------------------------------
  88.  
  89. FW_PPrivResourceFile::FW_PPrivResourceFile() :
  90.     FW_CCountedPtr()
  91. {
  92. }
  93.  
  94. //----------------------------------------------------------------------------------------
  95. // FW_PPrivResourceFile::FW_PPrivResourceFile
  96. //----------------------------------------------------------------------------------------
  97.  
  98. FW_PPrivResourceFile::FW_PPrivResourceFile(FW_CPrivResourceFileRep* privResourceFileRep) :
  99.     FW_CCountedPtr(privResourceFileRep)
  100. {
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_PPrivResourceFile::FW_PPrivResourceFile
  105. //----------------------------------------------------------------------------------------
  106.  
  107. FW_PPrivResourceFile::FW_PPrivResourceFile(const FW_PPrivResourceFile& privResourceFile) :
  108.     FW_CCountedPtr(privResourceFile)
  109. {
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. // FW_PPrivResourceFile::~FW_PPrivResourceFile
  114. //----------------------------------------------------------------------------------------
  115.  
  116. FW_PPrivResourceFile::~FW_PPrivResourceFile()
  117. {
  118. }
  119.  
  120. //========================================================================================
  121. // CLASS FW_CPrivResourceFileRep
  122. //========================================================================================
  123.  
  124. //----------------------------------------------------------------------------------------
  125. // FW_CPrivResourceFileRep::FW_CPrivResourceFileRep 
  126. //----------------------------------------------------------------------------------------
  127.  
  128. FW_CPrivResourceFileRep::FW_CPrivResourceFileRep(FW_ResourceFileID resFileID) :
  129.     FW_CCountedPtrRep(),
  130.     fMustCloseFile(FALSE),
  131.     fResourceFileID(resFileID),
  132.     fFileSpec(FW_CPrivResourceFileRep::GetFileSpecificationForID(resFileID))
  133. {
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // FW_CPrivResourceFileRep::FW_CPrivResourceFileRep 
  138. //----------------------------------------------------------------------------------------
  139.  
  140. FW_CPrivResourceFileRep::FW_CPrivResourceFileRep(const FW_CFileSpecification& fileSpec) :
  141.     FW_CCountedPtrRep(),
  142.     fMustCloseFile(TRUE),
  143.     fResourceFileID(FW_CPrivResourceFileRep::OpenResourceFile(fileSpec)),
  144.     fFileSpec(fileSpec)
  145. {
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. // FW_CPrivResourceFileRep::~FW_CPrivResourceFileRep
  150. //----------------------------------------------------------------------------------------
  151.  
  152. FW_CPrivResourceFileRep::~FW_CPrivResourceFileRep()
  153. {
  154.     if (fMustCloseFile)
  155.         FW_CPrivResourceFileRep::CloseResourceFile(fResourceFileID);
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. // FW_CPrivResourceFileRep::GetResource
  160. //
  161. //    Private method.  Attempts to load the resource, but will return NULL on failure.
  162. //----------------------------------------------------------------------------------------
  163.  
  164. FW_ResourceHandle FW_CPrivResourceFileRep::GetResource(
  165.                                                        FW_ResourceId resourceId,
  166.                                                        FW_ResourceType resourceType) const
  167. {
  168.     FW_ResourceHandle resourceHandle = NULL;
  169.  
  170. #if defined FW_BUILD_WIN
  171.     resourceHandle = ::FindResource(fResourceFileID, 
  172.                                     MAKEINTRESOURCE(resourceId), 
  173.                                     MAKEINTRESOURCE(resourceType));
  174.  
  175.     if(resourceHandle == 0)
  176.     {
  177.         char fileName[255] = {0};
  178.         ::GetModuleFileName(fResourceFileID, fileName, sizeof fileName);
  179.         __asm int 3
  180.     }
  181.  
  182. #elif defined FW_BUILD_MAC
  183.     // Load the resource.
  184.     short temp = CurResFile();
  185.     if (temp!=fResourceFileID) ::UseResFile(fResourceFileID);
  186.     resourceHandle = ::Get1Resource(resourceType, short(resourceId));    // Must cast!
  187.     if (temp!=fResourceFileID) ::UseResFile(temp);
  188. #endif
  189.  
  190.     return resourceHandle;
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // FW_CPrivResourceFileRep::GetResourceHandle
  195. //----------------------------------------------------------------------------------------
  196.  
  197. FW_ResourceHandle FW_CPrivResourceFileRep::GetResourceHandle(
  198.                                                              FW_ResourceId resourceId,
  199.                                                              FW_ResourceType resourceType) const
  200. {
  201.     FW_ResourceHandle handle = GetResource(resourceId, resourceType);
  202.  
  203.     // Throw an exception if the resource was not loaded.
  204.     ThrowExceptionIfResourceLoadError(handle, resourceId, resourceType);
  205.  
  206.     return handle;
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // FW_CPrivResourceFileRep::ReleaseResourceHandle
  211. //----------------------------------------------------------------------------------------
  212.  
  213. void FW_CPrivResourceFileRep::ReleaseResourceHandle(FW_ResourceHandle handle) const
  214. {
  215. #ifdef FW_BUILD_MAC
  216.     ::ReleaseResource(handle);
  217. #else
  218. FW_UNUSED(handle);
  219. #endif
  220. }
  221.  
  222. //----------------------------------------------------------------------------------------
  223. // FW_CPrivResourceFileRep::GetFileSpecification
  224. //----------------------------------------------------------------------------------------
  225.  
  226. const FW_CFileSpecification* FW_CPrivResourceFileRep::GetFileSpecification() const
  227. {
  228.     return (&fFileSpec);
  229. }
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // FW_CPrivResourceFileRep::ThrowExceptionIfResourceLoadError
  233. //----------------------------------------------------------------------------------------
  234.  
  235. void FW_CPrivResourceFileRep::ThrowExceptionIfResourceLoadError(
  236.                                                                 FW_PlatformHandle resourceHandle,
  237.                                                                 FW_ResourceId resourceId,
  238.                                                                 FW_ResourceType resourceType) const
  239. {
  240.     if (resourceHandle != NULL)
  241.         return;
  242.  
  243. #ifdef FW_BUILD_MAC
  244.     // Get the resource fError...       BEFORE DOING ANYTHING ELSE!
  245.     short resError = ::ResError();
  246.     if (resError == noErr) resError = resNotFound;
  247.     FW_Failure(resNotFound);
  248. #endif
  249.  
  250.     // Throw an exception
  251. #ifdef FW_BUILD_WIN
  252.     if (!HasResource(resourceId, resourceType))
  253.         FW_Failure(FW_xResourceNotFound);
  254.     FW_Failure(FW_xResourceNotLoaded);
  255. #endif
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. // FW_CPrivResourceFileRep::HasResource
  260. //----------------------------------------------------------------------------------------
  261.  
  262. FW_Boolean FW_CPrivResourceFileRep::HasResource(FW_ResourceId resourceId,
  263.                                                 FW_ResourceType resourceType) const
  264. {
  265. #ifdef FW_BUILD_WIN
  266.     FW_ResourceHandle resourceHandle;
  267.     resourceHandle = ::FindResource(fResourceFileID, 
  268.                                     MAKEINTRESOURCE(resourceId), 
  269.                                     MAKEINTRESOURCE(resourceType));
  270. #endif
  271.  
  272. #ifdef FW_BUILD_MAC
  273.     FW_CMacResLoadFalse dontLoadResource;
  274.     FW_PlatformHandle resourceHandle = GetResource(resourceId, resourceType);
  275. #endif
  276.  
  277.     return (resourceHandle != NULL);
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // FW_CPrivResourceFileRep::GetFileSpecificationForID
  282. //----------------------------------------------------------------------------------------
  283.  
  284. FW_CFileSpecification FW_CPrivResourceFileRep::GetFileSpecificationForID(FW_ResourceFileID resFileID)
  285. {
  286. #ifdef FW_BUILD_WIN
  287.     // Get file name from Windows
  288.     FW_Char buffer[255];
  289.     ::GetModuleFileName(resFileID, buffer, sizeof buffer);
  290.     FW_CString255 fileName(buffer);
  291. #elif defined FW_BUILD_MAC
  292.     FW_PascalChar buffer[255];
  293.     FCBPBRec    pb;
  294.     pb.ioCompletion = 0;
  295.     pb.ioFCBIndx = 0;
  296.     pb.ioVRefNum = 0;
  297.     pb.ioRefNum = resFileID;
  298.     pb.ioNamePtr = buffer;
  299.     OSErr err = ::PBGetFCBInfoSync(&pb);
  300.     if (err != noErr)
  301.         buffer[0] = 0;
  302.     FW_CString255 fileName((FW_Char *) buffer+1, buffer[0]);
  303. #endif
  304.  
  305.     // Return by value (copy constructor or operator= will be called)
  306.     return FW_CFileSpecification(fileName);
  307. }
  308.  
  309. //----------------------------------------------------------------------------------------
  310. // FW_CPrivResourceFileRep::OpenResourceFile
  311. //----------------------------------------------------------------------------------------
  312.  
  313. FW_ResourceFileID FW_CPrivResourceFileRep::OpenResourceFile(const FW_CFileSpecification& fileSpec)
  314. {
  315. #ifdef FW_BUILD_WIN
  316.     FW_CDynamicString fileName;
  317.     fileSpec.GetFullPath(fileName);
  318.     FW_ResourceFileID result = ::LoadLibrary(fileName);
  319.     
  320. #ifdef FW_BUILD_WIN16
  321.     if (result < HINSTANCE_ERROR)
  322.     {
  323.         int error = result;
  324. #endif
  325. #ifdef FW_BUILD_WIN32
  326.     if(result == NULL)
  327.     {
  328.         DWORD error = ::GetLastError();
  329. #endif
  330.         switch (error)
  331.         {
  332.             case 2:                                // File not found.
  333.             case 3:                                // Path not found.
  334.                 FW_Failure(FW_xResourceFileNotFound);
  335.             case 8:                                // Insufficient memory
  336.                 FW_Failure(FW_xMemoryExhausted);
  337.             default:
  338.                 FW_Failure(FW_xResourceFile);
  339.         }
  340. #ifdef FW_BUILD_WIN32
  341.     }
  342. #endif
  343. #ifdef FW_BUILD_WIN16
  344.     }
  345. #endif
  346. #endif
  347.  
  348. #ifdef FW_BUILD_MAC
  349.     FSSpec macSpec;
  350.     fileSpec.MacGetFSSpec(macSpec);
  351.     FW_ResourceFileID result = ::FSpOpenResFile(&macSpec, fsRdPerm);
  352.     if (result == -1)
  353.     {
  354.         short resError = ::ResError();
  355.         if (resError == noErr)
  356.             resError = resFNotFound;
  357.         FW_Failure(resError);
  358.     }
  359. #endif
  360.  
  361.     return result;
  362. }
  363.  
  364. //----------------------------------------------------------------------------------------
  365. // FW_CPrivResourceFileRep::CloseResourceFile
  366. //----------------------------------------------------------------------------------------
  367.  
  368. void FW_CPrivResourceFileRep::CloseResourceFile(FW_ResourceFileID file)
  369. {
  370. #if defined FW_BUILD_MAC
  371.     ::CloseResFile(file);
  372. #elif defined FW_BUILD_WIN
  373.     ::FreeLibrary(file);
  374. #endif
  375. }
  376.  
  377. //----------------------------------------------------------------------------------------
  378. // FW_CPrivResourceFileRep::PrivHasSpecialResource
  379. //----------------------------------------------------------------------------------------
  380.  
  381. FW_Boolean FW_CPrivResourceFileRep::PrivHasSpecialResource(FW_ResourceId resourceId,
  382.                                                           FW_ResourceType resourceType) const
  383. {
  384.     // (JEL) I can't find any reason why this function should be different from
  385.     // HasResource.  For now, I'll just call HasResource, but maybe later I'll
  386.     // have to do something else.
  387.     return HasResource(resourceId, resourceType);
  388. }
  389.  
  390. //----------------------------------------------------------------------------------------
  391. // FW_CPrivResourceFileRep::PrivGetSpecialResource
  392. //----------------------------------------------------------------------------------------
  393.  
  394. FW_PlatformHandle 
  395. FW_CPrivResourceFileRep::PrivGetSpecialResource(FW_ResourceId resourceId,
  396.                                                   FW_ResourceType resourceType) const
  397. {
  398.     FW_PlatformHandle handle = NULL;
  399.  
  400. #if defined FW_BUILD_MAC
  401.     // (JEL) ??? Some resource types are truly "special", in that calling GetResource
  402.     // to load them is a bad idea.  However, several types of resoures have their
  403.     // own GetXxxx toolbox call that is supposedly equivalent to just calling
  404.     // GetResource(XXXX, id).  For some of these types, we call the GetXxxx function,
  405.     // but we allow the default handler to get the rest of these types.  It bothers
  406.     // me a little that we're not consistent, but it seems like there is no good
  407.     // reason to worry about it.
  408.     switch(resourceType)
  409.     {
  410.         case FW_kCursor:
  411.             handle = (FW_PlatformHandle) ::GetCCursor(resourceId);
  412.             break;
  413.             
  414.         case FW_kIcon:
  415.             handle = (FW_PlatformHandle) ::GetCIcon(resourceId);
  416.             break;
  417.             
  418.         case FW_kBlackWhiteIcon:
  419.             handle = ::GetIcon(resourceId);
  420.             break;
  421.             
  422.         case FW_kBlackWhiteCursor:
  423.             handle = (FW_PlatformHandle) ::GetCursor(resourceId);
  424.             break;
  425.             
  426.         case FW_kPicture:
  427.             handle = (FW_PlatformHandle) ::GetPicture(resourceId);
  428.             break;
  429.  
  430.         case FW_kBitmap:
  431.             handle = ::GetResource(resourceType, resourceId);
  432.             break;
  433.             
  434.         default:
  435.             FW_DEBUG_MESSAGE("Not a special resource type");
  436.     }
  437. #elif defined FW_BUILD_WIN
  438.     switch(resourceType)
  439.     {
  440.         case FW_kCursor:
  441.             handle = ::LoadCursor(fResourceFileID, MAKEINTRESOURCE(resourceId));
  442.             break;
  443.             
  444.         case FW_kIcon:
  445.             handle = ::LoadIcon(fResourceFileID, MAKEINTRESOURCE(resourceId));
  446.             break;
  447.             
  448.         case FW_kBitmap:
  449.             handle = ::LoadBitmap(fResourceFileID, MAKEINTRESOURCE(resourceId));
  450.             break;
  451.             
  452.         case FW_kAccelerator:
  453.             handle = ::LoadAccelerators(fResourceFileID, MAKEINTRESOURCE(resourceId));
  454.             break;
  455.             
  456.         default:
  457.             FW_DEBUG_MESSAGE("Not a special resource type");
  458.     }
  459. #endif
  460.  
  461.     ThrowExceptionIfResourceLoadError(handle, resourceId, resourceType);
  462.     return handle;
  463. }
  464.  
  465.